﻿using MJPEGDecoderWinRT.Common;
using MJPEGDecoderWinRT.Model;
using MJPEGDecoderWinRT.View;
using System.Windows.Input;
using Windows.UI.Xaml.Controls;

namespace MJPEGDecoderWinRT.ViewModel
{
    public class MainPageViewModel: BindableBase
    {
        private Frame _frame;

        string _cameraName;
        string _cameraUri;
        string _cameraUserName;
        string _cameraPassword;
        string _cameraResourcePath;

        TRelayCommand<object> _showCameraPageCmd;

        public MainPageViewModel(Frame frame)
        {
            _frame = frame;
            _cameraName = "Test";
            _cameraUri = "http://192.168.0.4:8082";
            _cameraResourcePath = "video2.mjpg"; // MJPEG resource path for a Foxcam camera
        }

        #region Properties

        public string CameraName
        {
            get { return _cameraName; }
            set {
                SetProperty(ref _cameraName, value);
                _showCameraPageCmd.RaiseCanExecuteChanged();
            }
        }

        public string CameraUri
        {
            get { return _cameraUri; }
            set {
                SetProperty(ref _cameraUri, value);
                _showCameraPageCmd.RaiseCanExecuteChanged();
            }
        }

        public string CameraPassword
        {
            get { return _cameraPassword; }
            set { SetProperty(ref _cameraPassword, value); }
        }

        public string CameraUserName
        {
            get { return _cameraUserName; }
            set { SetProperty(ref _cameraUserName, value); }
        }

        public string CameraResourcePath
        {
            get { return _cameraResourcePath; }
            set { SetProperty(ref _cameraResourcePath, value); }
        }

        #endregion

        #region Commands

        public ICommand ShowCameraPageCmd
        {
            get
            {
                if (_showCameraPageCmd == null)
                {
                    _showCameraPageCmd = new TRelayCommand<object>(
                        // Action func
                        (o) =>
                        {
                            Camera camera = new Camera();
                            camera.Name = CameraName;
                            camera.Uri = CameraUri;
                            camera.UserName = CameraUserName;
                            camera.Password = CameraPassword;
                            camera.CameraResourcePath = CameraResourcePath;
                            ViewCameraPageViewModel viewModel = new ViewCameraPageViewModel(_frame, camera);
                            _frame.Navigate(typeof(ViewCameraPage), viewModel);
                        },
                        // CanExecute func
                        () =>
                        {
                            return !string.IsNullOrEmpty(CameraName) && !string.IsNullOrEmpty(CameraUri); 
                        });
                }
                return _showCameraPageCmd;
            }
        }

        #endregion

    }
}
